package com.sxpi.zixun.service.adminrole;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.AdminRoleBo;
import com.sxpi.zixun.common.enums.AdminUserResultEnum;
import com.sxpi.zixun.common.enums.ResultStatusEnum;
import com.sxpi.zixun.common.ex.PugValidatorException;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.mapper.AdminRoleMapper;
import com.sxpi.zixun.pojo.AdminRole;
import com.sxpi.zixun.vo.AdminRoleVo;
import org.apache.commons.lang3.StringUtils;
import org.pug.generator.anno.PugDoc;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 董伟豪
 * @Date: 2022/8/25 11:03
 */
@Service
public class IAdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements IAdminRoleService {

    /**
     * 查询全部角色
     * @return
     */
    @Override
    public List<AdminRoleBo> findAdminRoleList() {
        LambdaQueryWrapper<AdminRole> adminRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        adminRoleLambdaQueryWrapper.eq(AdminRole::getStatus,1);
        adminRoleLambdaQueryWrapper.eq(AdminRole::getIsdelete,0);
        return tranferListBo(this.list(adminRoleLambdaQueryWrapper),AdminRoleBo.class);
    }

    /**
     * 查询后台角色管理列表信息并分页
     * @param adminRoleVo
     * @return
     */
    @Override
    public IPage<AdminRoleBo> findAdminRolePage(AdminRoleVo adminRoleVo) {
        // 设置分页信息
        Page<AdminRole> adminRolePage = new Page<>(adminRoleVo.getPageNo(),adminRoleVo.getPageSize());
        // 设置查询条件
        LambdaQueryWrapper<AdminRole> adminRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询发布的 0 未发布  1 发布
        adminRoleLambdaQueryWrapper.eq(adminRoleVo.getStatus() != null,AdminRole::getStatus,adminRoleVo.getStatus());
        // 为了后期的回收站功能
        adminRoleLambdaQueryWrapper.eq(AdminRole::getIsdelete,0);
        // 多列搜索     太啰嗦了后期优化
        // adminRoleLambdaQueryWrapper.like(StringUtils.isNotEmpty(adminRoleVo.getKeyword()),AdminRole::getRoleName,adminRoleVo.getKeyword());
        adminRoleLambdaQueryWrapper.and(StringUtils.isNotEmpty(adminRoleVo.getKeyword()),wrapper -> {
            wrapper.like(AdminRole::getRoleName,adminRoleVo.getKeyword());
        });
        // 根据时间排降序
        adminRoleLambdaQueryWrapper.orderByDesc(AdminRole::getCreateTime);
        // 查询分页返回
        Page<AdminRole> page = this.page(adminRolePage, adminRoleLambdaQueryWrapper);
        return tranferPageBo(page,AdminRoleBo.class);
    }

    /**
     * 保存和修改后台角色管理
     * @param adminrole
     * @return
     */
    @Override
    public AdminRoleBo saveUpdateAdminRole(AdminRole adminrole) {
        // 还有一个问题，就是请勿输入重复的代号
        boolean flag = this.saveOrUpdate(adminrole);
        return flag ? tranferBo(adminrole, AdminRoleBo.class) : null;
    }

    /**
     * 根据后台角色管理id删除后台角色管理
     * @param id
     * @return
     */
    @Override
    public int deleteAdminRoleById(Long id) {
        AdminRole adminRole = this.getById(id);
        if(adminRole != null){
            AdminRole adminRole2 = new AdminRole();
            adminRole2.setId(id);
            adminRole2.setIsdelete(1);
            this.saveOrUpdate(adminRole2);
            return 1;
        }
        return 0;
    }

    /**
     * 查询全部角色对应的权限
     * @param roleId
     * @return
     */
    @Override
    public List<String> findPermissionByRoleId(Long roleId) {
        return this.baseMapper.findPermissionByRoleId(roleId);
    }

    /**
     * 保存角色的权限
     * @param roleId
     * @param permissionIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean savePermissionByRoleId(Long roleId, String permissionIds) {
        // 如果存在的话，那就删除角色全部的权限的权限
        if(roleId != null){
            this.baseMapper.deletePermissionByRoleId(roleId);
        }
        Vsserts.isEmptyEx(permissionIds, AdminUserResultEnum.USER_ROLE_AUTH_ERROR);
        String[] permissionIdsStr = permissionIds.split(",");
        for(String permissionIdStr : permissionIdsStr){
            this.baseMapper.savePermissionByRoleId(roleId,new Long(permissionIdStr));
        }
        return true;
    }

}
